package com.jason.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jason.system.common.util.Pager;
import com.jason.system.entity.Number;
import com.jason.system.entity.SolveOrder;
import com.jason.system.entity.Student;
import com.jason.system.entity.WorkLoad;
import com.jason.system.mapper.NumberMapper;
import com.jason.system.mapper.SolveOrderMapper;
import com.jason.system.mapper.StudentMapper;
import com.jason.system.service.NumberService;
import com.jason.system.service.SolveOrderService;
import com.jason.system.service.StudentService;
import com.jason.system.service.WorkLoadService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author jie
 * @version 1.0
 * success come from self-discipline
 * @date 2021/4/23 15:21
 */
@Service
public class WorkLoadServiceImpl implements WorkLoadService {

  @Resource
  private SolveOrderMapper solveOrderMapper;

  @Resource
  private NumberMapper numberMapper;

  @Resource
  private StudentMapper studentMapper;


  @Override
  public Pager<WorkLoad> getAllWorkLoad(Integer page, Integer size) {
    List<SolveOrder> solveOrderList = solveOrderMapper.getSolveOrders();
    List<WorkLoad>  workLoadList = new ArrayList<>();
    Map<String,WorkLoad> stringMap = new HashMap<>(8);
    //为学生添加派单的工作数量
    for (SolveOrder s : solveOrderList){
      String category = s.getOrder().getCategory().trim();
      List<Integer> sIds = new ArrayList<>();
      if(s.getMaintainer().contains(",")){
        sIds = Arrays.stream(s.getMaintainer().split(",")).map(s1 -> Integer.parseInt(s1.trim())).collect(Collectors.toList());
      }else {
        sIds.add(Integer.valueOf(s.getMaintainer()));
      }
      List<Student> students = studentMapper.getStudentByList(sIds);
      for (int i = 0 ;i<students.size();i++){
        String stuName = students.get(i).getStuName();
        if (!stringMap.containsKey(stuName)){
          WorkLoad w = new WorkLoad("",0,0,0,0);
          w.setStudentName(stuName);
          //派单类别为电信
          if(("电信").equals(category)){
            w.setTelecoms(1);
          }else if(("校园网").equals(category)){
            w.setCampusNetwork(1);
          }else{
            w.setOther(1);
          }
          stringMap.put(stuName,w);
          workLoadList.add(w);
        }else {
          WorkLoad wl = stringMap.get(stuName);
          //派单类别为电信
          if(("电信").equals(category)){
            wl.setTelecoms(wl.getTelecoms()+1);
            //类别为校园网
          }else if(("校园网").equals(category)){
            wl.setCampusNetwork(wl.getCampusNetwork()+1);
            //类别为维修平台
          } else {
            //类别为其它情况
            wl.setOther(wl.getOther()+1);
          }
        }
      }
    }
    //为学生添加电话的工作量
    List<Number> numbers = numberMapper.getAllNumbers();
    for (Number n :numbers){
      String stuName = n.getStuName();
        if (!stringMap.containsKey(stuName)){
          WorkLoad w = new WorkLoad("",0,0,0,0);
          w.setStudentName(stuName);
          w.setNumbers(1);
          stringMap.put(stuName,w);
          workLoadList.add(w);
        }else {
          WorkLoad wl = stringMap.get(stuName);
          if (wl.getNumbers() == null) {
            wl.setNumbers(1);
          } else {
            wl.setNumbers(wl.getNumbers() + 1);
          }
        }
    }
    // 这里放最开始时，会导致查出来11条数据，就只使用了10条
    PageHelper.startPage(page,size);
    PageInfo<WorkLoad> workLoadPageInfo = new PageInfo<>(workLoadList);
    return new Pager<>(workLoadPageInfo);
  }

  @Override
  public Pager<WorkLoad> getAllWorkLoadByTime(Integer page, Integer size, Date startTime, Date endTime) {
    List<SolveOrder> solveOrderList = solveOrderMapper.getSolveOrdersByTime(startTime,endTime);
    List<WorkLoad>  workLoadList = new ArrayList<>();
    Map<String,WorkLoad> stringMap = new HashMap<>(8);
    //为学生添加派单的工作数量
    for (SolveOrder s : solveOrderList){
      List<Integer> sIds = Arrays.stream(s.getMaintainer().split(",")).map(s1 -> Integer.parseInt(s1.trim())).collect(Collectors.toList());
      List<Student> students = studentMapper.getStudentByList(sIds);
      String category = s.getOrder().getCategory().trim();
      for (int i = 0 ;i<students.size();i++){
        String stuName = students.get(i).getStuName();
        if (!stringMap.containsKey(stuName)){
          WorkLoad w = new WorkLoad("",0,0,0,0);
          w.setStudentName(stuName);
          //派单类别为电信
          if(("电信").equals(category)){
            w.setTelecoms(1);
            //类别为校园网
          }else if(("校园网").equals(category)){
            w.setCampusNetwork(1);
            //类别为维修平台
          }else {
            //类别为其它情况
            w.setOther(1);
          }
          stringMap.put(stuName,w);
          workLoadList.add(w);
        }else {
          WorkLoad wl = stringMap.get(stuName);
          //派单类别为电信
          if(("电信").equals(category)){
            wl.setTelecoms(wl.getTelecoms()+1);
            //类别为校园网
          }else if(("校园网").equals(category)){
            wl.setCampusNetwork(wl.getCampusNetwork()+1);
            //类别为维修平台
          }else {
            //类别为其它情况
            wl.setOther(wl.getOther()+1);
          }
        }
      }
    }
    //为学生添加电话的工作量
    List<Number> numbers = numberMapper.getAllNumbersByTime(startTime,endTime);
    for (Number n :numbers){
        if (!stringMap.containsKey(n.getStuName())){
          WorkLoad w = new WorkLoad("",0,0,0,0);
          w.setStudentName(n.getStuName());
          w.setNumbers(1);
          workLoadList.add(w);
        }else {
          WorkLoad wl = stringMap.get(n.getStuName());
          wl.setNumbers(wl.getNumbers()+1);
      }
    }
    PageHelper.startPage(page,size);
    PageInfo<WorkLoad> workLoadPageInfo = new PageInfo<>(workLoadList);
    return new Pager<>(workLoadPageInfo);
  }
}
